package com.zl.basic.CommonUtil;

import java.util.Map;
import java.util.Set;

import com.thoughtworks.xstream.XStream;

/**
 * User: c3t
 * Date: 13-12-10
 * Time: 上午9:10
 */
public class XmlUtil {
	private static XmlUtil xmlUtil = new XmlUtil();
	private XmlUtil() {}

	public static XmlUtil getInstance() {
		return  xmlUtil;
	}

	public String obj2Xml(Object obj,Map<String,Class<?>> alias){
		Set<String> keys = alias.keySet();
		XStream xStream = new XStream();
		for(String key :keys) {
			xStream.alias(key,alias.get(key));
		}
		return xStream.toXML(obj);
	}

	public Object xml2Obj(String xml,Map<String,Class<?>> alias){
		Set<String> keys = alias.keySet();
		XStream xStream = new XStream();
		for(String key :keys) {
			xStream.alias(key,alias.get(key));
		}
		return xStream.fromXML(xml);
	}
}
